package com.team06.util;

import com.team06.pojo.Cfce;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.File;

public class CfceXmlParserUtil {
    public static Cfce parseXml(File xmlFile) {
        Cfce cfce = new Cfce();
        try {
            SAXBuilder saxBuilder = new SAXBuilder();
            Document document = saxBuilder.build(xmlFile);
            Element root = document.getRootElement();

            // 解析 META 节点
            Element meta = root.getChild("META");
            cfce.setMetaSnDR(meta.getChildText("SNDR"));
            cfce.setMetaRCVR(meta.getChildText("RCVR"));
            cfce.setMetaSEQN(Integer.parseInt(meta.getChildText("SEQN")));
            cfce.setMetaDDTM(meta.getChildText("DDTM"));
            cfce.setMetaTYPE(meta.getChildText("TYPE"));
            cfce.setMetaSTYP(meta.getChildText("STYP"));

            // 解析 DFLT 节点
            Element dflt = root.getChild("DFLT");
            cfce.setDfltFLID(dflt.getChildText("FLID"));
            cfce.setDfltFFID(dflt.getChildText("FFID"));
            cfce.setDfltFIDE(dflt.getChildText("FIDE"));
            cfce.setDfltCFTP(dflt.getChildText("CFTP"));
            cfce.setDfltCFNO(dflt.getChildText("CFNO"));

            return cfce;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("解析 Cfce XML 失败: " + xmlFile.getAbsolutePath(), e);
        }
    }
}